package com.lucky.back.service.impl;

import com.lucky.back.ex.ServiceException;
import com.lucky.back.mapper.CartMapper;
import com.lucky.back.pojo.dto.CartAddNew;
import com.lucky.back.pojo.entity.Cart;
import com.lucky.back.pojo.vo.CartListItemVO;
import com.lucky.back.pojo.vo.CartStandardVO;
import com.lucky.back.service.ICartService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service

public class CartServiceImpl implements ICartService {
    @Autowired
    CartMapper cartMapper;

    @Override
    public void addNew(CartAddNew cartAddNew) {
        if(cartMapper.getUserIdAndProductId(cartAddNew.getUserid(), cartAddNew.getProductid())!=null){
            Cart cart= cartMapper.getUserIdAndProductId(cartAddNew.getUserid(), cartAddNew.getProductid());
            int buyname = cart.getBuynum()+1;
            cart.setBuynum(buyname);
            cartMapper.updateById(cart);
            return;
        }
        Cart cart = new Cart();
        BeanUtils.copyProperties(cartAddNew,cart);
        System.out.println("即将执行插入service");
        cartMapper.insert(cart);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除请求");
        CartStandardVO queruResult = cartMapper.getStandardById(id);
        if (queruResult == null){
            String message = "删除失败,,尝试访问数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将删除数据");
        cartMapper.delete(id);
        log.debug("删除，完成！");
    }

    @Override
    public List<CartListItemVO> list(Long userid) {
        log.debug("查询种类列表服务层——————————————————");
        return cartMapper.list(userid);
    }

    @Override
    public void deleteByIds(List<Long> list) {
        log.debug("开始处理删除请求");
        cartMapper.deleteByIds(list);
        log.debug("删除分类，完成！");
    }
}
